package com.UnionPayCenter.controller;

import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.service.IComponentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @program: UnionPayCenter
 * @description: 跳转页面
 * @author: bite
 * @create:
 **/
@Slf4j
@Controller
@Api(value = "templates", tags = "跳转页面")
@RequestMapping("/templates")
public class TemplatesController {
    @Autowired
    private IComponentService componentService;

    /**
     * @Description: 接收授权码回调
     * @Param:
     * @return:
     * @Author: bite
     * @Date: 2019/7/21
     */
    @RequestMapping(value="/authCodeBack/{shopId}")
    @ApiOperation(tags = "微信回调", value = "[微信]接收授权码回调", response = String.class)
    public String wechatAuthCodeBack(@PathVariable(value = "shopId") Integer shopId,
                                     @RequestParam(value = "auth_code") String authCode,
                                    @RequestParam(value = "expires_in") String expiresIn) throws Exception {

        //获取授权码
        log.info(String.format("接收授权码回调 authCode:%s, expiresIn: %s", authCode, expiresIn));
        componentService.authCodeBack(shopId, authCode);

        try {
            componentService.openBinds(shopId);
        }catch (ApiException e){
            log.error(String.format("微信公众号/小程序绑定开放平台失败，失败原因：%s", e.getMessage()));
        }catch (Exception e){
            log.error(String.format("微信公众号/小程序绑定开放平台失败", e));
        }
        return "close";
    }


}
